const app = getApp();
import {request} from '../../util/request'
import Toast from '@vant/weapp/toast/toast';
import { store } from '../../store/store';
import {createStoreBindings} from 'mobx-miniprogram-bindings'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        height: 0,
        array: [{
                img: './img/one.png',
                name: '水果'
            },
            {
                img: './img/two.png',
                name: '蔬菜'
            },
            {
                img: './img/three.png',
                name: '肉禽蛋品'
            },
            {
                img: './img/four.png',
                name: '海鲜水产'
            },
            {
                img: './img/five.png',
                name: '粮油调味'
            },
            {
                img: './img/six.png',
                name: '熟食卤味'
            },
            {
                img: './img/seven.png',
                name: '冰品面点'
            },
            {
                img: './img/eight.png',
                name: '牛奶面包'
            },
            {
                img: './img/nine.png',
                name: '酒水冷饮'
            },
            {
                img: './img/ten.png',
                name: '休闲零食'
            }
        ],
        dress:'',
        list:[],
        items:['精选好货','今日特价','好吃省事','无肉不欢'],
        flag:false
    },         
    setContainerHeight: function (e) {
        //图片的原始宽度
        var imgWidth = e.detail.width;
        //图片的原始高度
        var imgHeight = e.detail.height;
        //同步获取设备宽度
        var sysInfo = wx.getSystemInfoSync();
        //获取屏幕的高度
        var screenWidth = sysInfo.screenWidth;
        //获取屏幕和原图的比例
        var scale = screenWidth / imgWidth;
        //设置容器的高度
        this.setData({
            height: imgHeight * scale
        })
    },
    getdata(){
        const params = {
            url:"http://localhost:8000/api/addgoods/all",
        }
     request(params).then(res=>{
         this.setData({list:this.data.list.concat(res.data)})
         store.addlist(res.data)
     })
    },
    loadMoreData(){
        if(!this.data.flag){
            this.setData({flag:true})
            Toast.loading({
                message: '加载中...',
                forbidClick: true,
                duration:2000,
                onClose: () => {
                console.log(123)
                 this.getdata()
                 this.setData({flag:false})
                  },
              });
        } 
    },
    detailAction(res){
        console.log(res)
        wx.navigateTo({
          url: '/pages/details/details?id='+res.currentTarget.dataset.id,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.storeBindings = createStoreBindings(this,{
            store,
            fields:['list'],
            action:['addlist']
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.getdata()
        var that = this;
        wx.getLocation({
            type: "wgs84",
            success(res) {
                wx.chooseLocation({
                    latitude: res.latitude,
                    longitude: res.longitude,
                    success:(data)=> {
                        console.log(data)
                        that.setData({dress:data.address})
                    }
                })
            }
        })
    },
    // onReady: function () {
    //     var that = this;
    //     wx.getLocation({
    //         type: "wgs84",
    //         success(res) {
    //             wx.chooseLocation({
    //                 latitude: res.latitude,
    //                 longitude: res.longitude,
    //                 success:(data)=> {
    //                     console.log(data)
    //                     that.setData({dress:data.address})
    //                 }
    //             })
    //         }
    //     })
    // },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.onLoad();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        this.storeBindings.destroyStoreBindings();
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        console.log(11)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        
    },
  

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})